/**
 * Implement some loaders for loading pictures. 
 * @author Songgang Xu (sxu at tamu dot edu)
 * @file picloader.h
 */


#ifndef _PICLOADER_H_
#define _PICLOADER_H_

/**
 * The class read PPM format file. 
 */
class PPM
{
public:
	PPM() {}
	friend std::istream & operator >> (std::istream & istr, PPM & ppm)
	{
		std::string str ;
		getline(istr, str); // read the first line
		getline(istr, str); // read the second line
		istr>> ppm.width >> ppm.height ; // read the third line
		getline(istr, str) ; // read the third line end
		getline(istr, str) ; // read the forth line

		ppm.data = new int_RGB * [ppm.height];
		for (unsigned i = 0; i < ppm.height; ++i)
		{
			ppm.data[i] = new int_RGB [ppm.width];
			for (unsigned j = 0; j< ppm.width ; ++j)
			{
				
				istr>>(ppm.data[i][j]).r >> ppm.data[i][j].g >> ppm.data[i][j].b; 

			}
		}
		return istr ;
	}
	virtual ~PPM() {this->finalize();}
	inline const int getWidth() const {return this->width ;}
	inline const int getHeight() const {return this->height ;}
	inline const int_RGB & getColor (unsigned x, unsigned y) const
	{
		return data[x][y];
	}
private:

	void finalize ()
	{
		for (unsigned i = 0;i < height; ++i)
		{
			delete []data[i];
		}
		delete []data;
	}
private:
	unsigned width, height ;
	int_RGB * * data ;
};

#endif
